package com.redis.demo.rabbitmq.Topics;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.redis.demo.rabbitmq.Constants.Constants;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {


            ConnectionFactory factory=new ConnectionFactory();
            factory.setHost(Constants.HOST);
            factory.setPort(Constants.PORT);
            factory.setUsername(Constants.USERNAME);
            factory.setPassword(Constants.PASSWORD);
            factory.setVirtualHost(Constants.VIRTUALHOST);
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();

            channel.exchangeDeclare(Constants.TOPIC_EXCHANGE, BuiltinExchangeType.TOPIC,true);
            channel.queueDeclare(Constants.TOPIC_QUEUE1,true,false,false,null);
            channel.queueDeclare(Constants.TOPIC_QUEUE2,true,false,false,null);

            //绑定交换机
            channel.queueBind(Constants.TOPIC_QUEUE1,Constants.TOPIC_EXCHANGE,"*.a.*");
            channel.queueBind(Constants.TOPIC_QUEUE2,Constants.TOPIC_EXCHANGE,"*.*.b");
            channel.queueBind(Constants.TOPIC_QUEUE2,Constants.TOPIC_EXCHANGE,"c.#");



            String msg_a="hello topic rabbitmqaaaaaaaaaaaa";
            channel.basicPublish(Constants.TOPIC_EXCHANGE ,"ae.a.f",null,msg_a.getBytes());//转发到q1
            String msg_b="hello rabbitmqbbbbbbbbbbbb";
            channel.basicPublish(Constants.TOPIC_EXCHANGE ,"ef.a.b",null,msg_b.getBytes());//转发到q2和q1
            String msg_c="hello rabbitmqcccccccccccc";
            channel.basicPublish(Constants.TOPIC_EXCHANGE ,"c.ef.d",null,msg_c.getBytes());
            channel.close();
            connection.close();





        }
    }
